﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dude.MLS;
using System.Xml.Serialization;
using System.ComponentModel;

namespace Dude.Data.Meta
{
    [Serializable]
    [XmlRoot(ElementName = "table")]
    public class TableInfo : ITreeReconstruct, IVisitedDataModel
    {
        public TableInfo()
        {
            Fields = new List<DataFieldInfo>();
        }

        [Category("General")]
        [XmlAttribute("name")]
        public string TableName { get; set; }

        [Category("General")]
        [XmlAttribute("dbname")]
        public string DBTableName { get; set; }

        [Category("Localization")]
        [XmlElement("display")]
        public MLString DisplayName { get; set; }

        [Category("Localization")]
        [XmlElement("desc")]
        public MLString Description { get; set; }

        [Category("Localization")]
        [XmlElement("longdesc")]
        public MLString LongDescription { get; set; }

        [Category("Table fields")]
        [XmlElement("field")]
        public List<DataFieldInfo> Fields { get; set; }

        [Browsable(false)]
        [XmlIgnore]
        public DatabaseInfo Database { get; set; }

        #region ITreeReconstruct Members

        public void ReconstructTree(object parent)
        {
            Database = (DatabaseInfo)parent;

            foreach (ITreeReconstruct r in Fields)
            {
                if (r != null)
                    r.ReconstructTree(this);
            }
        }

        #endregion

        public override string ToString()
        {
            return TableName;
        }

        #region IVisitedDataModel Members

        public void VisitedDataModel(IDataModelVisitor visitor)
        {
            visitor.VisitTableInfo(this);
        }

        #endregion
    }
}
